(function () {
  function _onParseCommentRecent(item, atomClass) {
    const itemURL = util.url(item.url, item.atomLanguage, atomClass);
    return `
<li class="media">
    <div class="media-body">
      <h5 class="media-heading">
        <a target="_blank" href="${itemURL}#comments">Re: ${util.escapeHtml(item.atomName)}</a>
      </h5>
      <div class="body">${item.h_summary}</div>
      <div class="user">-- ${util.escapeHtml(item.h_userName)}</div>
    </div>
</li>
  `;
  }

  function _onParseTag(item) {
    const tagURL = `${util.url('static/articles.html')}?tagId=${item.id}&tagName=${encodeURIComponent(item.tagName)}`;
    return `
<li class="media">
  <div class="media-body">
    <div class="media-heading">
      <a href="${tagURL}">${util.escapeHtml(item.tagName)}</a>
    </div>
  </div>
  <div class="media-right"><span class="badge badge-secondary">${item.tagAtomCount}</span></div>
</li>
  `;
  }

  function _onParseProfileAuthor(data) {
    const author = data.user;
    const imgSrc = util.combineImageUrl(author.avatar, 48, 48);
    return `
  <div>
    <a class='avatar'>
      <img class="avatar avatar48" src="${imgSrc}" title="${util.escapeHtml(author.userName)}"/>
    </a>
    <span class='name'><a>${util.escapeHtml(author.userName)}</a></span>
    <div class="motto">${util.escapeHtml(author.motto) || ''}</div>
  </div>
  `;
  }

  function _onParseArticleRecent(item, atomClass) {
    const audio = !item.audioFirst ? '' : '<i class="fas fa-music"></i> ';
    const attachment = item.attachmentCount === 0 ? '' : '<i class="fas fa-paperclip"></i> ';
    // avatar
    const userAvatar = item.avatar || util.url('plugins/cms-pluginsidebar/assets/images/user.png');
    const itemURL = util.url(item.url, item.atomLanguage, atomClass);
    const stat = `
<div class="title stat no-parse" data-article-id="${item.atomId}">
<img class="avatar avatar16" src="${util.combineImageUrl(userAvatar, 16, 16)}">
<span>${util.escapeHtml(item.userName)}</span>
<span class="num date">${util.time.formatDateTime(item.updatedAt, 'MM-DD HH:mm')}</span>
<i class="fas fa-eye"></i><span class="num readCount">${item.readCount}</span>
<i class="fas fa-heart"></i><span class="num starCount">${item.starCount}</span>
<a target="_blank" href="${itemURL}#comments">
  <i class="fas fa-comment"></i>
  <span class="num commentCount">${item.commentCount}</span>
</a>
</div>
        `;
    return `
<li class="media">
    <div class="media-body">
      <h4 class="media-heading">
        ${audio}${attachment}
        <a target="_blank" href="${itemURL}">${util.escapeHtml(item.atomName)}</a>
      </h4>
      ${util.escapeHtml(item.description) || item.summary}
      ${stat}
    </div>
</li>
        `;
  }

  // sidebar
  util.sidebar = {
    tags(options) {
      const $container = $(options.container);
      if ($container.length === 0) return;
      if (!options.onParse) options.onParse = _onParseTag;
      this._loadTags({ $container, onParse: options.onParse });
    },
    getTagName(tagId) {
      const tag = window._tags.list.find(item => item.id === tagId);
      return tag ? tag.tagName : '';
    },
    commentsRecent(options) {
      const $containers = $(options.container);
      if ($containers.length === 0) return;
      for (let i = 0; i < $containers.length; i++) {
        const $container = $($containers[i]);
        if (!options.onParse) options.onParse = _onParseCommentRecent;
        this._loadCommentsRecent({ $container, onParse: options.onParse });
      }
    },
    profileAuthor(options) {
      const $container = $(options.container);
      if ($container.length === 0) return;
      if (!options.onParse) options.onParse = _onParseProfileAuthor;
      this._loadProfileAuthor({ $container, onParse: options.onParse });
    },
    articlePost(options) {
      const $container = $(options.container);
      if ($container.length === 0) return;
      $('.button-article-post', $container).click(() => {
        const item = {
          module: env.site.atomClass.module,
          atomClassName: env.site.atomClass.atomClassName,
        };
        if (env.language) {
          item.language = env.language.current;
        }
        const url = `${env.site.serverUrl}/#!/a/cms/article/post?item=${encodeURIComponent(JSON.stringify(item))}`;
        location.assign(url);
      });
    },
    articlesRecent(options) {
      const $containers = $(options.container);
      if ($containers.length === 0) return;
      for (let i = 0; i < $containers.length; i++) {
        const $container = $($containers[i]);
        if (!options.onParse) options.onParse = _onParseArticleRecent;
        this._loadArticlesRecent({ $container, onParse: options.onParse });
      }
    },
    _loadArticlesRecent({ $container, onParse }) {
      let atomClass;
      if ($container.data('module')) {
        atomClass = {
          module: $container.data('module'),
          atomClassName: $container.data('atomclassname'),
        };
      } else {
        atomClass = env.site.atomClass;
      }
      // options
      const options = {
        language: env.language && env.language.current,
        where: {},
        orders: [['a.createdAt', 'desc']],
        page: { index: 0, size: env.article2.recentNum },
        mode: 'default',
      };
      // select
      util
        .performAction({
          method: 'post',
          url: '/a/cms/article/list',
          body: {
            atomClass,
            options,
          },
        })
        .then(data => {
          const $list = $('.list', $container);
          for (let i = 0; i < data.list.length; i++) {
            $list.append($(onParse(data.list[i], atomClass)));
          }
        });
    },
    _loadTags({ $container, onParse }) {
      const $list = $('.list', $container);
      for (let i = 0; i < window._tags.list.length; i++) {
        $list.append($(onParse(window._tags.list[i])));
      }
    },
    _loadCommentsRecent({ $container, onParse }) {
      let atomClass;
      if ($container.data('module')) {
        atomClass = {
          module: $container.data('module'),
          atomClassName: $container.data('atomclassname'),
        };
      } else {
        atomClass = env.site.atomClass;
      }
      // options
      const options = {
        orders: [['h_updatedAt', 'desc']],
        page: { index: 0, size: env.comment.recentNum },
      };
      // select
      util
        .performAction({
          method: 'post',
          url: '/a/cms/comment/all',
          body: {
            atomClass,
            options,
          },
        })
        .then(data => {
          const $list = $('.list', $container);
          for (let i = 0; i < data.list.length; i++) {
            $list.append($(onParse(data.list[i], atomClass)));
          }
        });
    },
    _loadProfileAuthor({ $container, onParse }) {
      // profile
      util
        .performAction({
          method: 'post',
          url: '/a/user/public/profile',
          body: {
            userId: env.article.userIdCreated,
          },
        })
        .then(data => {
          const $panelBody = $('.card-body', $container);
          $panelBody.append($(onParse(data)));
        });
    },
  };
})();
